﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace BLL
{
    public class T_BOrder
    {
        //省份
        public static List<Area> Save()
        {
            SqlDataReader dr = T_Order.Area();
            List<Area> save = new List<Area>();
            while (dr.Read())
            {
                Area a = new Area();
                a.codeID = (string)dr["codeID"];
                a.name = (string)dr["name"];
                a.parentID = (string)dr["parentID"];
                save.Add(a);
            }
            dr.Close();
            return save;
        }
        //查询市
        public static List<Area> City(string i)
        {
            SqlDataReader dr = T_Order.City(i);
            List<Area> City = new List<Area>();
            while (dr.Read())
            {
                Area a = new Area();
                a.codeID = (string)dr["codeID"];
                a.name = (string)dr["name"];
                a.parentID = (string)dr["parentID"];
                City.Add(a);
            }
            dr.Close();
            return City;
        }
        //查询县
        public static List<Area> County(string i)
        {
            SqlDataReader dr = T_Order.County(i);
            List<Area> County = new List<Area>();
            while (dr.Read())
            {
                Area a = new Area();
                a.codeID = (string)dr["codeID"];
                a.name = (string)dr["name"];
                a.parentID = (string)dr["parentID"];
                County.Add(a);
            }
            dr.Close();
            return County;
        }
        //添加地址
        public static bool AddAddress(ShoppingAddress ads)
        {
            return T_Order.AddAddress(ads);
        }
        //查询地址
        public static List<ShoppingAddress> Address(int id)
        {
            SqlDataReader dr = T_Order.Address(id);
            List<ShoppingAddress> adr = new List<ShoppingAddress>();
            while (dr.Read())
            {
                ShoppingAddress s = new ShoppingAddress();
                s.Saveid = Cha((string)dr["Saveid"]);
                s.AddressId = (int)dr["AddressId"];
                s.UserID = (int)dr["UserID"];
                s.Cityid = Cha((string)dr["Cityid"]);
                s.Countyid = Cha((string)dr["Countyid"]);
                s.AddressValue = (string)dr["AddressValue"];
                s.Name = (string)dr["Name"];
                s.phone = (string)dr["phone"];
                s.AddressState = (int)dr["AddressState"];
                adr.Add(s);
            }
            dr.Close();
            return adr;
        }
        //查询详细地址
        public static string Cha(string i)
        {
             SqlDataReader dr = T_Order.Cha(i);
             string a = "";
             while (dr.Read())
             {
                 a = (string)dr["name"];
             }
             dr.Close();
             return a;
        }

        //查询选中地址
        public static List<ShoppingAddress> Ucha(int i)
        {
            SqlDataReader dr = T_Order.Ucha(i);
            List<ShoppingAddress> adr = new List<ShoppingAddress>();
            while (dr.Read())
            {
                ShoppingAddress s = new ShoppingAddress();
                s.Saveid = Cha((string)dr["Saveid"]);
                s.AddressId = (int)dr["AddressId"];
                s.UserID = (int)dr["UserID"];
                s.Cityid = Cha((string)dr["Cityid"]);
                s.Countyid = Cha((string)dr["Countyid"]);
                s.AddressValue = (string)dr["AddressValue"];
                s.Name = (string)dr["Name"];
                s.phone = (string)dr["phone"];
                s.AddressState = (int)dr["AddressState"];
                adr.Add(s);
            }
            dr.Close();
            return adr;
        }
        //查询默认地址
        public static List<ShoppingAddress> Cham(int id)
        {
            SqlDataReader dr = T_Order.Cham(id);
            List<ShoppingAddress> adr = new List<ShoppingAddress>();
            while (dr.Read())
            {
                ShoppingAddress s = new ShoppingAddress();
                s.Saveid = Cha((string)dr["Saveid"]);
                s.AddressId = (int)dr["AddressId"];
                s.UserID = (int)dr["UserID"];
                s.Cityid = Cha((string)dr["Cityid"]);
                s.Countyid = Cha((string)dr["Countyid"]);
                s.AddressValue = (string)dr["AddressValue"];
                s.Name = (string)dr["Name"];
                s.phone = (string)dr["phone"];
                s.AddressState = (int)dr["AddressState"];
                adr.Add(s);
            }
            dr.Close();
            return adr;
        }

        //修改地址
        public static bool UpdataAddress(ShoppingAddress sh)
        {
            return T_Order.UpdataAddress(sh);
        }

        //删除用户地址
        public static bool DelAddress(int id)
        {
            return T_Order.DelAddress(id);
        }

        //设置默认地址
        public static bool Addressm(int id)
        {
            return T_Order.Addressm(id);
        }

        //生成订单
        public static bool OrderInfo(OrderInformatica oi)
        {
            return T_Order.OrderInfo(oi);
        }

        //添加商品订单
        public static bool AddOrder(List<OrderDetails> odeta, OrderInformatica oinfo)
        {
            return T_Order.AddOrder(odeta, oinfo);
        }

        //查询购物详情
        public static List<CartItem> OrderInfo(string[] i)
        {
            SqlDataReader dr = T_Order.OrderInfo(i);
            List<CartItem> item = new List<CartItem>();
            while (dr.Read())
            {
                CartItem s = new CartItem();
                s.Id = (int)dr["Id"];
                s.CartId = (int)dr["CartId"];
                s.Num = (int)dr["Num"];
                s.BookId = (int)dr["BookId"];
                s.Price = (decimal)dr["Price"];
                s.Img = (string)dr["Img"];
                s.BookName = (string)dr["BookName"];
                item.Add(s);
            }
            dr.Close();
            return item;
        }
    }
}
